package com.ss.android.article.base.feature.detail.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.account.Spipe;
import com.ss.android.action.a.a.Comment;
import com.ss.android.article.base.feature.detail.model.CommentWrapper;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class ArticleTabCommentsRequestTask extends AbsApiThread {
    private Context mContext;
    private Handler mHandler;
    private CommentFetcher mCommentFetcher;

    public ArticleTabCommentsRequestTask(Context context, Handler handler, CommentFetcher commentFetcher) {
        this.mContext = context.getApplicationContext();
        this.mHandler = handler;
        this.mCommentFetcher = commentFetcher;
    }

    private static void parseComment(JSONArray jsonArray, List<CommentWrapper> commentWrapperList, long groupId) throws JSONException {
        if (jsonArray != null) {
            int length = jsonArray.length();
            for (int i = 0; i < length; ++i) {
                CommentWrapper commentWrapper = CommentWrapper.extractFromJO(jsonArray.getJSONObject(i));
                if (commentWrapper != null) {
                    commentWrapperList.add(commentWrapper);
                    if (commentWrapper.mComment != null && commentWrapper.mComment.groupId <= 0) {
                        commentWrapper.mComment.groupId = groupId;
                    }
                }
            }
        }
    }

    public static boolean requestComments(Context context, CommentFetcher commentFetcher) {
        try {
            int v0_2;
            if (NetworkUtils.isNetworkAvailable(context)) {
                try {
                    StringBuilder urlBuilder = new StringBuilder(Spipe.articleV1TabCommentsUrl);
                    urlBuilder.append("?group_id=").append(commentFetcher.groupId);
                    if (commentFetcher.article != null) {
                        urlBuilder.append("&item_id=").append(commentFetcher.article.itemId);
                        urlBuilder.append("&aggr_type=").append(commentFetcher.article.aggrType);
                    }

                    urlBuilder.append("&count=20");
                    urlBuilder.append("&offset=").append(commentFetcher.offset);
                    urlBuilder.append("&tab_index=").append(commentFetcher.tabIndex);
                    if (commentFetcher.topCommentId > 0) {
                        urlBuilder.append("&top_comment_id=").append(commentFetcher.topCommentId);
                    }

                    if (commentFetcher.zzcomment != null && !commentFetcher.zzcomment.isEmpty()) {
                        urlBuilder.append("&zzids=");
                        Iterator<Comment> iterator = commentFetcher.zzcomment.iterator();
                        while (true) {
                            if (iterator.hasNext()) {
                                Object v0_3 = iterator.next();
                                if (v0_3 == null) {
                                    continue;
                                }
                                urlBuilder.append(((Comment) v0_3).commentId).append(",");
                                continue;
                            } else {
                                v0_2 = 44;
                                if (v0_2 == urlBuilder.charAt(urlBuilder.length() - 1)) {
                                    urlBuilder.deleteCharAt(urlBuilder.length() - 1);
                                }
                            }
                        }
                    }
                    String resp = NetworkUtils.doGet(204800, urlBuilder.toString());
                    if (resp != null && resp.length() != 0) {
                        JSONObject jo = new JSONObject(resp);
                        resp = jo.getString("message");
                        if (!"success".equals(resp)) {
                            Logger.w("snssdk", "get_comments status: " + resp);
                            commentFetcher.errorCode = 18;
                            return false;
                        } else {
                            ArticleTabComment articleTabComment = new ArticleTabComment();
                            commentFetcher.articleTabComment = articleTabComment;
                            JSONArray dataJA = jo.optJSONArray("data");
                            commentFetcher.j = dataJA != null ? dataJA.length() : 0;
                            ArticleTabCommentsRequestTask.parseComment(dataJA, articleTabComment.commentWrapperList, commentFetcher.groupId);
                            articleTabComment.hasMore = AbsApiThread.getHasMore(jo, false);
                            articleTabComment.goTopicDetail = AbsApiThread.optBoolean(jo, "go_topic_detail", false);
                            articleTabComment.totalNumber = jo.optInt("total_number", -1);
                            articleTabComment.banComment = AbsApiThread.optBoolean(jo, "ban_comment", false);
                            articleTabComment.detailNoComment = AbsApiThread.optBoolean(jo, "detail_no_comment", false);
                            articleTabComment.showAddForum = jo.optInt("show_add_forum");
                            JSONObject tabInfoJO = jo.optJSONObject("tab_info");
                            if (tabInfoJO != null) {
                                JSONArray tabJA = tabInfoJO.optJSONArray("tabs");
                                int v10 = 2;
                                if (tabJA != null && tabJA.length() >= v10) {
                                    ArrayList<String> v4_2 = new ArrayList();
                                    for (int i = 0; i < v10; ++i) {
                                        String v5_1 = tabJA.getString(i);
                                        if (!android.support.design.a.isTextEmpty(v5_1)) {
                                            v4_2.add(v5_1);
                                        }
                                    }
                                    if (v4_2.size() != v10) {
                                        return true;
                                    }
                                    commentFetcher.i = new String[2];
                                    commentFetcher.i[0] = v4_2.get(0);
                                    commentFetcher.i[1] = v4_2.get(1);
                                }
                            }

                            return true;
                        }
                    }
                    commentFetcher.errorCode = 18;
                    return false;
                } catch (Throwable throwable) {
                    commentFetcher.errorCode = TTUtils.getErrorCode(context, throwable);
                    throwable.printStackTrace();
                    return false;
                }
            } else {
                commentFetcher.errorCode = 12;
                return false;
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
        return false;
    }

    public final void run() {
        Message message = ArticleTabCommentsRequestTask.requestComments(this.mContext, this.mCommentFetcher)
                ? this.mHandler.obtainMessage(1003, this.mCommentFetcher)
                : this.mHandler.obtainMessage(1004, this.mCommentFetcher);
        this.mHandler.sendMessage(message);
    }
}

